// pages/profile/profile.js
const app = getApp();

Page({
  data: {
    userInfo: null,
    isVip: false,
    statistics: {
      totalCalculations: 0,
      totalAdViews: 0,
      totalAdClicks: 0
    },
    settings: {
      theme: 'light',
      notifications: true,
      autoSave: true
    }
  },

  onLoad() {
    this.loadUserInfo();
  },

  onShow() {
    this.loadUserInfo();
  },

  // 加载用户信息
  loadUserInfo() {
    const userInfo = app.globalData.userInfo;
    const isVip = app.globalData.isVip;
    
    if (userInfo) {
      this.setData({
        userInfo,
        isVip,
        statistics: userInfo.statistics || {
          totalCalculations: 0,
          totalAdViews: 0,
          totalAdClicks: 0
        },
        settings: userInfo.preferences || {
          theme: 'light',
          notifications: true,
          autoSave: true
        }
      });
    }
  },

  // 用户登录
  login() {
    app.showLoading('登录中...');
    
    app.login().then((userInfo) => {
      app.hideLoading();
      this.setData({
        userInfo,
        isVip: false,
        statistics: userInfo.statistics,
        settings: userInfo.preferences
      });
      app.showSuccess('登录成功');
    }).catch((error) => {
      app.hideLoading();
      console.error('登录失败:', error);
      app.showError('登录失败，请重试');
    });
  },

  // 退出登录
  logout() {
    wx.showModal({
      title: '确认退出',
      content: '确定要退出登录吗？',
      success: (res) => {
        if (res.confirm) {
          app.logout();
          this.setData({
            userInfo: null,
            isVip: false,
            statistics: {
              totalCalculations: 0,
              totalAdViews: 0,
              totalAdClicks: 0
            }
          });
        }
      }
    });
  },

  // 跳转到VIP页面
  goToVip() {
    wx.navigateTo({
      url: '/pages/vip/vip'
    });
  },

  // 跳转到设置页面
  goToSettings() {
    wx.navigateTo({
      url: '/pages/settings/settings'
    });
  },

  // 跳转到关于页面
  goToAbout() {
    wx.navigateTo({
      url: '/pages/about/about'
    });
  },

  // 跳转到帮助页面
  goToHelp() {
    wx.navigateTo({
      url: '/pages/help/help'
    });
  },

  // 跳转到反馈页面
  goToFeedback() {
    wx.navigateTo({
      url: '/pages/feedback/feedback'
    });
  },

  // 切换主题
  toggleTheme() {
    const newTheme = this.data.settings.theme === 'light' ? 'dark' : 'light';
    this.updateSetting('theme', newTheme);
  },

  // 切换通知设置
  toggleNotifications() {
    const newValue = !this.data.settings.notifications;
    this.updateSetting('notifications', newValue);
  },

  // 切换自动保存
  toggleAutoSave() {
    const newValue = !this.data.settings.autoSave;
    this.updateSetting('autoSave', newValue);
  },

  // 更新设置
  updateSetting(key, value) {
    const settings = { ...this.data.settings };
    settings[key] = value;
    
    this.setData({ settings });
    
    // 保存到用户信息
    if (this.data.userInfo) {
      const userInfo = { ...this.data.userInfo };
      userInfo.preferences = settings;
      
      wx.setStorageSync('userInfo', userInfo);
      app.globalData.userInfo = userInfo;
      
      this.setData({ userInfo });
    }
  },

  // 清除缓存
  clearCache() {
    wx.showModal({
      title: '确认清除',
      content: '确定要清除所有缓存数据吗？',
      success: (res) => {
        if (res.confirm) {
          this.confirmClearCache();
        }
      }
    });
  },

  // 确认清除缓存
  confirmClearCache() {
    app.showLoading('清除中...');
    
    try {
      // 清除历史记录
      wx.removeStorageSync('calculationHistory');
      
      // 重置统计
      if (this.data.userInfo) {
        const userInfo = { ...this.data.userInfo };
        userInfo.statistics = {
          totalCalculations: 0,
          totalAdViews: 0,
          totalAdClicks: 0
        };
        
        wx.setStorageSync('userInfo', userInfo);
        app.globalData.userInfo = userInfo;
        
        this.setData({
          userInfo,
          statistics: userInfo.statistics
        });
      }
      
      app.hideLoading();
      app.showSuccess('清除成功');
    } catch (error) {
      app.hideLoading();
      console.error('清除缓存失败:', error);
      app.showError('清除失败');
    }
  },

  // 分享应用
  shareApp() {
    wx.showShareMenu({
      withShareTicket: true,
      menus: ['shareAppMessage', 'shareTimeline']
    });
  },

  // 联系客服
  contactService() {
    wx.showModal({
      title: '联系客服',
      content: '客服微信：mortgage_calculator\n工作时间：9:00-18:00',
      showCancel: false
    });
  },

  // 分享给朋友
  onShareAppMessage() {
    return {
      title: '房贷计算器 - 专业计算工具',
      path: '/pages/index/index',
      imageUrl: '/images/share.jpg'
    };
  },

  // 分享到朋友圈
  onShareTimeline() {
    return {
      title: '房贷计算器 - 专业计算工具',
      imageUrl: '/images/share.jpg'
    };
  }
}); 